/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package iamutkarshtiwari.github.io.ananas.editimage.utils;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * 自定义toast
 */
public class MyToast {

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param resId   stringId
     */
    public static void show(Context context, int resId) {
        String msg = null;
        try {
            msg = context.getResourceManager().getElement(resId).getString();
        } catch (IOException | WrongTypeException | NotExistException e) {
            e.printStackTrace();
            msg = "未知错误！";
        }
        show(context, msg, 1500);
    }

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param msg   消息内容
     */
    public static void show(Context context, String msg) {
        show(context, msg, 1500);
    }

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param msg   消息内容
     * @param time   显示时间
     */
    public static void show(Context context, String msg, int time) {
        new ToastDialog(context).setText(msg).setDuration(time).show();
    }

    /**
     * 显示 dialog
     *
     * @param context Context
     * @param msg   消息内容
     */
    public static void show2(Context context, String msg) {
        ToastDialog toastDialog = new ToastDialog(context);
        Component parse = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_toast, null, false);
        Text text = (Text) parse.findComponentById(ResourceTable.Id_textview);
        text.setText(msg);
        toastDialog.setComponent(parse);
        toastDialog.setDuration(1000).show();
    }

}
